<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\8\21 0021
 * Time: 10:16
 */
namespace jingshan;

class Mredis
{
    /*这里替换为实例id和实例password*/
    //基础参数准备
    protected $host = "r-uf68b058e8f4f264.redis.rds.aliyuncs.com";
//    protected $host = "127.0.0.1";
    protected $port = 6379;
    protected $user = 'r-uf68b058e8f4f264';
    protected $pwd = 'Li02131421';
    protected $max = 1000;
    protected $key = 'shop';
    //设置数据库
    protected $redis;

    //构造函数 连接数据库
    public function __construct()
    {
        $this->redis = new \Redis();
        /*连接内网redis数据库*/
        if ($this->redis->connect($this->host,$this->port) == false)
        {
            die($this->redis->getLastError());
        }
        if ($this->host!='127.0.0.1')
        {
            /*user:password 拼接成AUTH的密码*/
            if ($this->redis->auth($this->user.":".$this->pwd) == false)
            {
                die($this->redis->getLastError());
            }
        }
    }

    //插入数值
    public function set_value($value)
    {
        //设置基数标志位
        if (!$this->redis->get('flag'))
        {
            $this->redis->set('flag',0);
        }

        //检测溢出
        if ($this->redis->get('flag')>$this->max)
        {
            return 'overflow';
        }

        //插入非重复数据
        if ($this->redis->zAdd($this->key,$this->redis->get('flag'),$value))
        {
            $this->redis->incr("flag");
        }
    }

    //返回全部存储数据
    public function get_value()
    {
        return $this->redis->zRange($this->key,0,-1);
    }

    //测试新增
    //返回计数器数据
    public function get_number()
    {
        return $this->redis->get("flag")?:0;
    }

    //清空数据库
    public function deleteall()
    {
        return $this->redis->flushDB();
    }
}